
package integral

import (
"errors"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"higame/config"
mongodb "higame/modules/db"
)

var db = mongodb.GetInstance()
var colName = config.Db.Collection.IntegralTask

type TaskSchema struct {
	ID       primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
	AddTime  int64              `json:"add_time" bson:"add_time" form:"add_time"`
	TaskID   string             `json:"task_id" bson:"task_id" form:"task_id"`
	TaskName string             `json:"task_name" bson:"task_name" form:"task_name"`
	Title    string             `json:"title" bson:"title" form:"title"`
	Subtitle string             `json:"subtitle" bson:"subtitle" form:"subtitle"`
	EXP      int64              `json:"exp" bson:"exp" form:"exp" binding:"required"`
	Integral int64              `json:"integral" bson:"integral" form:"integral" binding:"required"`
	IsLoop   bool               `json:"is_loop" bson:"is_loop" form:"is_loop"`
}
type PrizeSchema struct {
	Name   string  `json:"name" bson:"name" form:"name" binding:"required"`
	Title  string  `json:"title" bson:"title" form:"title"`
	Odds   float64 `json:"odds" bson:"odds" form:"odds" binding:"required"`
	Prize  int64   `json:"prize" bson:"prize" form:"prize" binding:"required"`
	Remark string  `json:"remark" bson:"remark" form:"remark"`
}

func NewTaskSchema() *TaskSchema {
	return &TaskSchema{}
}

func Parse(data bson.M) (*TaskSchema, error) {
	u := NewTaskSchema()
	if err := db.ToStruct(data, u); err != nil {
		return nil, err
	}
	return u, nil
}

func DeleteOneByTaskID(taskId string) error {
	b := db.DeleteOne(colName, bson.M{"task_id": taskId})
	if !b {
		return errors.New("删除失败")
	}
	return nil
}

func AddOne(schema *TaskSchema) error {
	adminI := db.InsertOne(colName, schema)
	if !adminI {
		return fmt.Errorf("添加失败。\n")
	}
	return nil
}

func FindOneByTaskID(taskId string) (*TaskSchema, error) {
	userF := db.FindOne(colName, bson.M{"task_id": taskId})
	if userF == nil {
		return nil, errors.New("数据不存在！")
	}
	return Parse(userF)
}

func FindOneByTaskName(taskName string) (*TaskSchema, error) {
	userF := db.FindOne(colName, bson.M{"task_name": taskName})
	if userF == nil {
		return nil, errors.New("数据不存在！")
	}

	return Parse(userF)
}

func FindMany(pageNum int64, filter interface{}) ([]bson.M, error) {
	pageNum = pageNum - 1
	if pageNum < 0 {
		pageNum = 0
	}
	opts := new(options.FindOptions)
	list := db.FindMany(colName, filter,
		opts.SetSkip(pageNum*20),
		opts.SetLimit(20),
		opts.SetSort(bson.M{
			"add_time": 1,
		}))
	return list, nil
}

func UpdateOneByTaskID(taskId string, update interface{}) error {
	b := db.UpdateOne(colName, bson.M{
		"task_id": taskId,
	}, bson.M{
		"$set": update,
	})

	if !b {
		return errors.New("没有修改任何数据")
	}
	return nil
}

func Count(filter interface{}) (int64, error) {
	return db.Count(colName, filter), nil
}



func FindPrize() (*[]PrizeSchema, error) {
	opts := new(options.FindOptions)
	list := db.FindMany(config.Db.Collection.Prizes, bson.M{},
		opts.SetSkip(0),
		opts.SetLimit(20),
		opts.SetSort(bson.M{
			"add_time": 1,
		}))
	var prizes = make([]PrizeSchema, 0)
	for _, v := range list {
		prize := &PrizeSchema{}
		if err := db.ToStruct(v, prize); err == nil {
			prizes = append(prizes, *prize)
		}
	}
	return &prizes, nil
}

func FindOnePrize(name string) (*PrizeSchema, error) {
	v := db.FindOne(config.Db.Collection.Prizes, bson.M{
		"name": name,
	})
	prize := &PrizeSchema{}
	if err := db.ToStruct(v, prize); err != nil {
		return nil, err
	}
	return prize, nil
}

func UpdateOneByPrizeName(name string, update interface{}) error {
	b := db.UpdateOne(config.Db.Collection.Prizes, bson.M{
		"name": name,
	}, bson.M{
		"$set": update,
	})

	if !b {
		return errors.New("没有修改任何数据")
	}
	return nil
}
